package com.itheima.communication;

/**
 * @author Anomaly
 * @date 2023/2/10 15:46
 */
public class Account {
    private double money;

    public Account() {
    }

    public Account(double money) {
        this.money = money;
    }

    public double getMoney() {
        return money;
    }

    public void setMoney(double money) {
        this.money = money;
    }


    public synchronized void drawMoney(double money) {
        try {
            String name = Thread.currentThread().getName();
            if (this.money >= money) {
                System.out.println(name + "成功取出：" + money);
                this.money -= money;
                System.out.println(name + "取完了，账户当前剩余：" + this.money);
                this.notifyAll();     //唤醒所有线程
                this.wait();        //让出锁，当前线程进入等待
            } else {
                this.notifyAll();
                this.wait();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public synchronized void depositMoney(double money) {
        try {
            String name = Thread.currentThread().getName();
            if (this.money==0){
                System.out.println(name + "成功存入：" + money);
                this.money += money;
                System.out.println(name + "存完了，账户当前剩余：" + this.money);
                this.notifyAll();
                this.wait();
            }else {
                this.notifyAll();
                this.wait();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
